import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class DioDemoBuilder extends StatefulWidget {
  const DioDemoBuilder({Key? key}) : super(key: key);

  @override
  _DioDemoBuilderState createState() => _DioDemoBuilderState();
}

class _DioDemoBuilderState extends State<DioDemoBuilder> {
  _getData() async {
    var response = await Dio().get("https://jdmall.itying.com/api/pcate");

    return response.data['result'];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Dio Demo Builder"),
        centerTitle: true,
      ),
      body: FutureBuilder(
          future: _getData(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasData) {
                List list = snapshot.data as List;
                return ListView(
                  children: list.map((item) {
                    return Column(
                      children: [
                        ListTile(title: Text("${item['title']}")),
                        const Divider()
                      ],
                    );
                  }).toList(),
                );
              } else {
                return const Center(
                  child: Text("没有数据啦..."),
                );
              }
            } else {
              return const Center(
                child: CircularProgressIndicator(color: Colors.purpleAccent),
              );
            }
          }),
    );
  }
}
